﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;


namespace EasyFolderShare
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbdBrowse.ShowDialog() == DialogResult.OK)
            {
                fbdBrowse.Description = "Select a folder to share";
                txtFolderPath.Text = fbdBrowse.SelectedPath;
                checkShare();
            }
        }
        public static int ExecuteCommand(string Command, int Timeout)
        {
            int ExitCode;
            ProcessStartInfo ProcessInfo;
            Process Process;

            ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = false;
            Process = Process.Start(ProcessInfo);
            Process.WaitForExit(Timeout);
            ExitCode = Process.ExitCode;
            Process.Close();

            return ExitCode;
        }

        private void btnStartSharing_Click(object sender, EventArgs e)
        {
            string command = "NET SHARE sharename=\"" + txtFolderPath.Text + "\" /Unlimited";
            ExecuteCommand(command, 6000);
            checkShare();
        }

        private void btnStopSharing_Click(object sender, EventArgs e)
        {
            string command = "NET SHARE \"" + txtFolderPath.Text + "\" /DELETE";
            ExecuteCommand(command, 6000);
            checkShare();
        }

        private void checkShare()
        {
            List<string> sharedFolders;
            sharedFolders = GetSharedFolders();
            foreach (string folder in sharedFolders){
                //MessageBox.Show("folder = " + txtFolderPath.Text + ", share = " + folder);
                if (folder == txtFolderPath.Text)
                {
                    lblStatus.ForeColor = Color.Green;
                    lblStatus.Text = "Shared";
                    break;
                }
                else
                {
                    lblStatus.ForeColor = Color.Red;
                    lblStatus.Text = "Not shared";
                }
            }
        }
        public static List<string> GetSharedFolders()
        {

            List<string> sharedFolders = new List<string>();

            // Object to query the WMI Win32_Share API for shared files...

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share");

            ManagementClass mc = new ManagementClass("Win32_Share"); //for local shares

            foreach (ManagementObject share in searcher.Get())
            {

                string type = share["Type"].ToString();

                if (type == "0") // 0 = DiskDrive (1 = Print Queue, 2 = Device, 3 = IPH)
                {
                    string name = share["Name"].ToString(); //getting share name

                    string path = share["Path"].ToString(); //getting share path

                    string caption = share["Caption"].ToString(); //getting share description

                    sharedFolders.Add(path);
                }

            }

            return sharedFolders;

        }

        private void cmdExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
